<!DOCTYPE html>
<html>
<head>
<title>Shop Online</title>
<?php
	session_start();
	include '../views/_meta.php';
	include '../models/Customer.php';
	require_once '../models/_check.php';
	require_once '../models/_check_ssCart.php';
	$error="";
	
?>
</head>
<body>
<form method="post" action="">
	<div id="firstLine">
		<div id="top-bar">
		<?php include '../views/_top_bar.php';?>
		</div>
	</div>
	<div id="container">
		<div id="header">
			<?php include '../views/_head.php';?>
		</div>
		<?php include '_menu_head.php';?>
		<div id="center">
			<div id="menu-left">
				<?php include '_menu_cart_customer.php';?>
			</div>
			
			<div id="table">
					<table id="table-customer">
						<tr>
							<th colspan="2">Chọn phương thức thanh toán</th>
						</tr>
						<tr> <td colspan="2" class="color_red">Số dư hiện có trong tài khoản là:<?php echo number_format($row['money'],3,".",".")?> Xu</td></tr>
						<tr> <td colspan="2" class="color_red">Tổng số tiền cần thanh toán là :<?php echo number_format($_SESSION["total"],3,".",".")?> Xu</td></tr>
						<tr>
							<td><Input type = 'Radio' name ='xu' id='xu' value= 'xu'><label for="xu">Thanh toán bằng tiền ảo</label></td>
							<td><Input type = 'Radio' name ='xu' id= 'tienmat' value= 'tienmat' ><label for="tienmat">Thanh toán khi nhận hàng</label></td>
						</tr>
						<tr>
						 <td><a href='#'><input type='submit' name='btn_submit_pm' class='btn_button_' id="btn_button" value='Thanh toán' /></a></td>
						<td><a href='address.php'><input type='button' class='btn_button_' value='Quay lại'></a></td>
						</tr>
				</table>
			</div>
			<div id="menu-left">
				<?php include '_menu_left.php';?>
			</div>
			
			
	</div>
	</div>
	<div id="footer">
		<?php include '../views/_footer.php';?>
	</div>
</form>
</body>
</html>
<?php 
	if(isset($_POST['btn_submit_pm']) )
	{
	if(isset($_POST['xu'])){
	if($_POST['xu']=='xu'){
	$row=Customer::getCustomerInfo($_SESSION["session_customer_id"]);
	if($_SESSION["total"]<=$row['money'])
	{$total=$_SESSION["total"];			
		$money=$row['money']-$_SESSION["total"];
		$re=Customer::payment($_SESSION["session_customer_id"],$money);
		if($re)
		{
			$check=true;
			if($check==true){
					$res=Product::addOrders($_SESSION["session_customer_id"],$total);
					foreach($_SESSION['cart'] as $key=>$value){
					$get=Product::getInfoProduct($key);
					$gets=Product::getInfoOrders_();
					$res=Product::addOrdersDetail($gets['ordersID'],$key,$value,$get['price']);
					if($res){
						unset($_SESSION['cart']);
						echo "	<script type='text/javascript'>
                               $(document).ready(function() {
								showNotification({
                                        type : 'information',
                                        message: 'Vui lòng đợi trong giây lát.Yêu cầu đang được xử lý.'
                                    });
							 })                            
                            </script>";	
							//load lai trang
						echo "<meta http-equiv='refresh' content='3;url=index.php' />";
					}else{
						echo "	<script type='text/javascript'>
                               $(document).ready(function() {
								showNotification({
                                        type : 'error',
                                        message: 'Giao dich thất bại.'
                                    });
							 })                            
                            </script>";	
					}
				}
			}
		}
		else {
						echo "	<script type='text/javascript'>
                               $(document).ready(function() {
								showNotification({
                                        type : 'error',
                                        message: 'Giao dich thất bại.'
                                    });
							 })                            
                            </script>";	
					}
	}else  echo "	<script type='text/javascript'>
                               $(document).ready(function() {
								showNotification({
                                        type : 'warning',
                                        message: 'Số dư trong tài khoản không đủ, vui lòng nạp thêm tiền.'
                                    });
							 })                            
                            </script>";
	}else if($_POST['xu']=='tienmat'){
	$total=$_SESSION["total"];
			$check=true;
			if($check==true){
				//foreach($_SESSION['cart'] as $key=>$value){
				//$get=Product::getInfoProduct($key);
					//$res=Product::addOrders($key,$_SESSION["session_customer_id"],$value,$get['price'],$total);
					$res=Product::addOrders($_SESSION["session_customer_id"],$total);
					foreach($_SESSION['cart'] as $key=>$value){
					$get=Product::getInfoProduct($key);
					$gets=Product::getInfoOrders_();
					$res=Product::addOrdersDetail($gets['ordersID'],$key,$value,$get['price']);
					if($res){
						unset($_SESSION['cart']);
						echo "	<script type='text/javascript'>
                               $(document).ready(function() {
								showNotification({
                                        type : 'information',
                                        message: 'Vui lòng đợi trong giây lát.Yêu cầu đang được xử lý.'
                                    });
							 })                            
                            </script>";	
							//load lai trang
						echo "<meta http-equiv='refresh' content='2;url=index.php' />";
					}else{
						echo "	<script type='text/javascript'>
                               $(document).ready(function() {
								showNotification({
                                        type : 'error',
                                        message: 'Giao dich thất bại.'
                                    });
							 })                            
                            </script>";	
					}
				
			}
		}
		
		
		}
		}
		
		else {
						echo "	<script type='text/javascript'>
                               $(document).ready(function() {
								showNotification({
                                        type : 'error',
                                        message: 'Bạn chưa chọn phương thức thanh toán.'
                                    });
							 })                            
                            </script>";	
					}
		}
	
?>
